#include	"Core.hpp"

Core::Core()
{
  initscr();
  cbreak();
  noecho();
  curs_set(0);
  keypad(stdscr, TRUE);/* I need that nifty F1 */

  start_color();/* Start color */
  init_pair(1, COLOR_BG_FOCUS, COLOR_FG_FOCUS);
  init_pair(2, COLOR_BG_CURSOR, COLOR_FG_CURSOR);
  init_pair(3, COLOR_YELLOW, COLOR_GREEN);
  init_pair(4, COLOR_GREEN, COLOR_YELLOW);
}

Core::~Core()
{
  endwin();
}

void	Core::updateAllWindows()
{
  this->_winExplorer->update((this->eventManager->getFocusState() == FOCUS_WINDOW_EXPLORER) ? true : false);
  this->_windowCode->update((this->eventManager->getFocusState() == FOCUS_WINDOW_CODE) ? true : false);
  this->_winWarning->update((this->eventManager->getFocusState() == FOCUS_WINDOW_WARNING) ? true : false);
}

void	Core::updateSingleWindow()
{
  if (this->eventManager->getFocusState() == FOCUS_WINDOW_CODE)
    {
      this->_windowCode->update(true);
      this->_winWarning->update(false);
    }
  else if (this->eventManager->getFocusState() == FOCUS_WINDOW_EXPLORER)
    this->_winExplorer->update(true);
}

void	Core::run()
{
  this->eventManager = new EventManager(this->_windowCode, this->_winExplorer, this->_winWarning);
  
  updateAllWindows();
  while (1)
    {
      if (this->eventManager->getNextEvent())
	{
	  wclear(this->_windowCode->getWindow());
	  updateAllWindows();
	}
      else
	updateSingleWindow();
    }
}

void	Core::initWindows()
{
  this->_windowCode = new WindowCode((unsigned int)80, (unsigned int)70);
  this->_windowCode->createSubWindows();
  
  this->_winExplorer = new WindowExplorer(this->_windowCode->getHeightWin(), this->_windowCode->getWidthtWin());
  
  this->_windowCode->setContentWin(this->_winExplorer->getLineOnCursor().c_str());
  
  
  this->_winWarning = new WindowWarning(this->_windowCode->getHeightWin(), this->_windowCode->getWidthtWin());
  this->_winWarning->setContentWin(this->_winExplorer->getLineOnCursor().c_str(), this->_windowCode->getSubWinCode()->getContentTree()->getList().size());
  keypad(this->_windowCode->getWindow(), true);
  keypad(this->_winWarning->getWindow(), true);
  keypad(this->_winExplorer->getWindow(), true);
}
